$(function() {

    //begin列表信息
    $("#grade_datagrid").datagrid({
        url: "/grade/list",
        columns: [[
            {field: "xxx", checkbox: 'true'},
            {field: "id", title: "编号", width: 100,hidden:true},
            {field: "name", title: "班级名称", width: 100},
            {field: "headTeacher", title: "班主任", width: 100,formatter:function(value,row,index){
                if(row.headTeacher){
                    return row.headTeacher.username;
                }
            }},
            {field: "classRoom", title: "所在教室", width: 100,formatter:function(value,row,index){
                if(value){
                    return value.name;
                }
            }},
            {field: "state", title: "状态", width: 100,formatter:function(value,row,index){
                if(value == 0){
                    return "<font color='red'>未毕业</font>";
                }else if(value == 1){
                    return "<font color='green'>已毕业</font>";
                }else if(value == 2){
                    return "<font color='blue'>未开班</font>";
                }
            }}
        ]],
        fit: true,
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        striped: true,
        pageNumber:1,
        pageSize:3,
        pageList:[3,5,10,20],
        toolbar: "#grade_tb"
    });
    //end列表信息

    //begin添加/编辑对话框
    $("#grade_dialog").dialog({
        width: 500,
        height: 500,
        buttons: "#grade_bb",
        closed: true
    });
    //end添加/编辑对话框

    //begin处理功能方法的对象
    var obj = {
        //begin添加按钮
        add: function () {
            $("#grade_form").form("clear");
            $("#grade_dialog").dialog("setTitle", "班级添加");
            $("#grade_dialog").dialog("open");
        },
        //end添加按钮

        //begin编辑按钮
        edit: function () {
            $("#grade_form").form("clear");
            $("#grade_dialog").dialog("setTitle", "班级编辑");
            var row = $("#grade_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            if(row.headTeacher){
                row["headTeacher.id"]=row.headTeacher.id;
            }
            if(row.classRoom){
                row["classRoom.id"]=row.classRoom.id;
            }
            $("#grade_form").form("load", row);
            $("#grade_dialog").dialog("open");
        },
        //end编辑按钮

        //begin取消按钮
        cancel: function () {
            $("#grade_dialog").dialog("close");
        },
        //end取消按钮

        //begin保存按钮
        save: function () {
            var gradeId = $("#gradeId").val();
            var url;
            if (gradeId) {
                url = "/grade/update"
            } else {
                url = "/grade/save"
            }
            $("#grade_form").form("submit", {
                url: url,
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);
                    } else {
                        $.messager.alert('温馨提示', "操作成功!");
                        $("#grade_dialog").dialog("close");
                        $("#grade_datagrid").datagrid("reload");
                    }
                },


            });
           /* $("#headTeacher_id").combobox({
              onSubmit:function(par){

              }
            }),*/




        },
        //end保存按钮

        //begin删除按钮
        remove:function(){
            var row = $("#grade_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            $.messager.confirm('确认','您确认要操作此行吗？',function(r){
                if (r){
                    //注意这里的id要从当前选中的行中获取
                    var gradeId = row.id;
                    $.get("/grade/delete",{gradeId:gradeId},function(data){
                        if(!data.success){
                            $.messager.alert('温馨提示', data.msg);
                        }else{
                            $.messager.alert('温馨提示', '操作成功!');
                            $("#grade_datagrid").datagrid("reload");
                        }
                    });
                }
            });

        },
        //end删除按钮

        //begin刷新按钮
        reload:function (){
            $("#grade_datagrid").datagrid("reload")
        },
        //end刷新按钮

        //begin高级查询按钮
        queryKeyWord:function (){
            var gradeName=$("#gradeName").textbox("getValue");
           var headTeacher=$("#headTeacher").combobox("getValue");

            $("#grade_datagrid").datagrid("load",{
                gradeName:gradeName,
                headTeacher:headTeacher,
            })
        }
        //end高级查询按钮
    };
    //end处理功能方法的对象

    //begin处理功能方法对象,实际调用方法
    $("a[data-cmd]").click(function(){
        var cmd = $(this).data("cmd");
        obj[cmd]();
    });
    //end处理功能方法对象,实际调用方法





});